home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / WTEK0593.ZIP;1 / PALETTE.ZIP / BLUEPAL.C next >
Encoding:
C/C++ Source or Header  |  1993-04-09  |  6.7 KB  |  206 lines

  1. #include <windows.h>
  2.  
  3. HPALETTE BuildPalette(void);
  4. long FAR PASCAL WndProc (HWND hWnd, WORD iMessage, WORD wParam, LONG lParam);
  5. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
  6. {
  7.     static HWND hWnd;
  8.     MSG Message;
  9.     WNDCLASS WndClass;
  10.  
  11.     if (!hPrevInstance)
  12.     {
  13.         WndClass.cbClsExtra = 0;
  14.         WndClass.cbWndExtra = 0;
  15.         WndClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  16.         WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
  17.         WndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  18.         WndClass.hInstance = hInstance;
  19.         WndClass.lpfnWndProc = (WNDPROC)WndProc;
  20.         WndClass.lpszClassName = "BLUE";
  21.         WndClass.lpszMenuName = NULL;
  22.         WndClass.style = CS_HREDRAW | CS_VREDRAW;
  23.         
  24.         RegisterClass (&WndClass);
  25.     }
  26.     
  27.     hWnd = CreateWindow ("BLUE", "Color Example",
  28.          WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
  29.          0, CW_USEDEFAULT, 0, NULL, NULL,
  30.          hInstance, NULL);
  31.     
  32.     ShowWindow (hWnd, nCmdShow);
  33.     while (GetMessage (&Message, 0, 0, 0))
  34.     {
  35.         TranslateMessage(&Message);
  36.         DispatchMessage(&Message);
  37.     }
  38.     return Message.wParam;
  39. }
  40.  
  41. /************************************************/
  42. /*          Window Procedure :  WndProc         */
  43. /************************************************/
  44.  
  45. long FAR PASCAL WndProc (HWND hWnd, WORD iMessage, WORD wParam, LONG lParam)
  46. {
  47.  
  48.     //**** Device Context variable ****/
  49.     HDC hDC;
  50.     
  51.     //**** Pens and brushes ****/
  52.     LOGPEN lpBlue;                                  // Logical outline pen.
  53.     LOGBRUSH lbBlue;                                // Logical fill brush.
  54.     static HPEN hBluePen, hOldPen;                  // Actual new and previous pens.
  55.     static HBRUSH hBlueBrush, hOldBrush;            // Actual new and previous brushes.
  56.     PAINTSTRUCT PtStr;                              // Something used by the Begin/EndPaint routines.
  57.     
  58.     
  59.     //**** General variables ****/
  60.     short x;                                        // Generic counter for loops.
  61.     int curcolor, mapped;                           // Current Color.
  62.     
  63.     //**** Palette variables ****/
  64.     static HPALETTE hPal, hOldPal;                  // Our palette and the previous one.
  65.     
  66.     switch (iMessage)
  67.     {
  68.         case WM_CREATE:
  69.             hPal = BuildPalette();
  70.             return(0);
  71.     
  72.     
  73.         case WM_PAINT:
  74.             lpBlue.lopnStyle = PS_SOLID;
  75.             lpBlue.lopnWidth.x = 1;
  76.             lpBlue.lopnWidth.y = 0; // Not used
  77.     
  78.             lbBlue.lbStyle = BS_SOLID;
  79.             lbBlue.lbHatch = 0;     // ignored when BS_SOLID
  80.                                      
  81.             hDC = BeginPaint(hWnd, &PtStr);
  82.     
  83.                 hOldPal = SelectPalette(hDC, hPal, FALSE);
  84.                 if (!hOldPal)
  85.                     MessageBox (NULL, "Couldn't Select Palette!", "BluePal", MB_OK);
  86.                 else
  87.                     RealizePalette(hDC);
  88.     
  89.                 for (x=1, curcolor=0; x<640; x+=16, curcolor+=6)
  90.                 {
  91.                     //*** Reference our palette indirectly:
  92.                       lpBlue.lopnColor = PALETTERGB(0,0,curcolor);
  93.                       lbBlue.lbColor = PALETTERGB(0,0,curcolor);
  94.     
  95.                     //*** Create outline pen and fill brush:
  96.                       hBluePen = CreatePenIndirect(&lpBlue);
  97.                       hBlueBrush = CreateBrushIndirect(&lbBlue);
  98.     
  99.                     //*** Tell hDC to use new pen&brush:
  100.                       hOldPen = SelectObject (hDC, hBluePen);
  101.                       hOldBrush = SelectObject (hDC, hBlueBrush);
  102.     
  103.                     //*** Draw a cute picture: ***/
  104.                       RoundRect (hDC,x,100,x+16,200,10,10);
  105.     
  106.                     //*** De-allocate current pen and brush.
  107.                       SelectObject (hDC, hOldPen);
  108.                       SelectObject (hDC, hOldBrush);
  109.                       DeleteObject(hBluePen);
  110.                       DeleteObject(hBlueBrush);
  111.                 }
  112.     
  113.                 SelectPalette(hDC, hOldPal, FALSE);      // Give back old palette.
  114.     
  115.     
  116.             EndPaint(hWnd, &PtStr);                      // Tell GDI we're done with device context.
  117.             return (0);
  118.      
  119.         case WM_PALETTECHANGED:
  120.             if ((HWND) wParam == hWnd)
  121.                 return 0;
  122.  
  123.             /* Otherwise, fall through to WM_QUERYNEWPALETTE. */
  124.  
  125.         case WM_QUERYNEWPALETTE:
  126.  
  127.         /*
  128.          * If realizing the palette causes the palette to change,
  129.          * redraw completely.
  130.          */
  131.  
  132.             hDC = GetDC(hWnd);
  133.             hOldPal = SelectPalette (hDC, hPal, FALSE);
  134.  
  135.  
  136.             mapped = RealizePalette(hDC); /* i == entries that changed  */
  137.  
  138.             SelectPalette (hDC, hPal, FALSE);
  139.             ReleaseDC(hWnd, hDC);
  140.  
  141.  
  142.             /* If any palette entries changed, repaint the window. */
  143.  
  144.             if (mapped > 0)
  145.                 InvalidateRect(hWnd, NULL, TRUE);
  146.  
  147.             return mapped;
  148.         case WM_DESTROY:
  149.             DeleteObject (hPal);                         // Free a little memory.
  150.             PostQuitMessage(0);
  151.             return (0);
  152.         default:
  153.             return (DefWindowProc (hWnd, iMessage, wParam, lParam));
  154.     }
  155. }
  156.  
  157.  
  158.  
  159.  
  160. HPALETTE BuildPalette(void)
  161. {
  162.     /**** Palette variables ****/
  163.     static HLOCAL hLocal;                           // Handle to memory allocated for logpalette.
  164.     static NPLOGPALETTE npLogPal;                   // Pointer to logpalette after the memory is locked.
  165.     int curcolor;                                   // Current intensity of blue.
  166.     short x;                                        // Generic counter for loops.
  167.     static HPALETTE hPal;                           // Palette handle we will return.
  168.     
  169.     // Allocate memory for logical palette.
  170.     hLocal = LocalAlloc(LPTR, sizeof(LOGPALETTE) + 40 * sizeof(PALETTEENTRY));
  171.     if (!hLocal)
  172.     {
  173.         MessageBox (NULL, "Couldn't Allocate Memory!", "BluePal", MB_OK);
  174.         PostQuitMessage(0);
  175.         return (0);
  176.     }
  177.     npLogPal = (NPLOGPALETTE) LocalLock(hLocal);
  178.     npLogPal->palVersion = 0x300;
  179.     npLogPal->palNumEntries = 40;
  180.  
  181.     // Fill in the colors.
  182.     curcolor = 0;
  183.     for (x=0, curcolor=0; x<40; x++, curcolor=curcolor+6)
  184.     {
  185.         npLogPal->palPalEntry[x].peBlue = (BYTE)curcolor;
  186.         npLogPal->palPalEntry[x].peRed = 0;
  187.         npLogPal->palPalEntry[x].peGreen = 0;
  188.         npLogPal->palPalEntry[x].peFlags = (BYTE)0;
  189.     }
  190.  
  191.     // Create the palette.
  192.     hPal = CreatePalette((LPLOGPALETTE)npLogPal);
  193.     if (!hPal) MessageBox (NULL, "Can't create palette!", "BluePal", MB_OK);
  194.  
  195.     //Free our data structure.
  196.     LocalUnlock (hLocal);
  197.     LocalFree (hLocal);
  198.  
  199.  
  200.     return (hPal);
  201.  
  202. }
  203.  
  204.  
  205.  
  206.